package com.fastCms.business.blueorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.blueorder.daos.BlueDeviceMapper;
import com.fastCms.business.blueorder.pojo.entity.BlueDevice;
import com.fastCms.business.blueorder.pojo.po.BlueDevicePO;
import com.fastCms.business.blueorder.pojo.vo.BlueDeviceVO;
import com.fastCms.business.blueorder.service.BlueDeviceService;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.frame.manage.CloudUserManage;
import com.fastCms.business.frame.manage.UserManage;
import com.fastCms.business.frame.pojo.po.IdStrPO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.fastCms.business.frame.utils.CommonsUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-10-17
 */
@Service
public class BlueDeviceServiceImpl extends ServiceImpl<BlueDeviceMapper, BlueDevice> implements BlueDeviceService {

    @Override
    public List<BlueDeviceVO> showList() {
        String organizeCode = CloudUserManage.getOrganizeCode();
        LambdaQueryWrapper<BlueDevice> bannerQueryWrapper = new LambdaQueryWrapper();
        bannerQueryWrapper.eq(BlueDevice::getOrganizeCode, organizeCode);
        bannerQueryWrapper.eq(BlueDevice::getState, CommonConst.STATE.VALID);
        List<BlueDevice> list = list(bannerQueryWrapper);
        if (!CollUtil.isEmpty(list)) {
            List<BlueDeviceVO> bannerVOList = BeanConvertUtil.convertList(list, BlueDeviceVO.class);
            return bannerVOList;
        }
        return Lists.newArrayList();
    }

    @Override
    public Boolean saveOrUpdate(BlueDevicePO param) throws BusinessException {
        if (StringUtils.isBlank(param.getId())) {
            BlueDevice blueDevice = new BlueDevice();
            CommonsUtil.initEntity(blueDevice);
            BeanUtil.copyProperties(param, blueDevice);
            blueDevice.setOrganizeCode(CloudUserManage.getOrganizeCode());
            this.save(blueDevice);
        } else {
            BlueDevice blueDevice = getById(param.getId());
            if (blueDevice == null) {
                throw new BusinessException("数据不存在");
            }
            BeanUtil.copyProperties(param, blueDevice);
            this.updateById(blueDevice);
        }
        return true;
    }

    @Override
    public ApiResponse<Boolean> delete(IdStrPO param) throws BusinessException {
        BlueDevice blueDevice = getById(param.getId());
        if (blueDevice == null) {
            throw new BusinessException("数据不存在");
        }
        blueDevice.setState(CommonConst.STATE.INVALID);
        this.updateById(blueDevice);
        return ResponseUtil.success(true);
    }

}
